package com.singlemusic.utils;
import java.security.SecureRandom;  

import javax.annotation.Resource;
import javax.crypto.Cipher;  
import javax.crypto.KeyGenerator;  
import javax.crypto.SecretKey;  

import org.springframework.beans.factory.annotation.Autowired;
  
  
  
public class AESUtils {        
    
	//密钥
	private static String key="singlemusic";
	/** 
     * 加密函数 
     * @param content   加密的内容 
     * @return          加密结果
     */  
    public static String enCrypt(String content) {  
        KeyGenerator keygen;          
        SecretKey desKey;  
        Cipher c;         
        byte[] cByte = null;  
        String str = content;  
        try {
        	keygen = KeyGenerator.getInstance("AES");  
            keygen.init(128, new SecureRandom(key.getBytes()));  
              
            desKey = keygen.generateKey();        
            c = Cipher.getInstance("AES");  
              
            c.init(Cipher.ENCRYPT_MODE, desKey);  
              
            cByte = c.doFinal(str.getBytes("UTF-8")); 
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}  
        return parseByte2HexStr(cByte);  
    }  
      
    /** 解密函数 
     * @param content 加密内容
     * @return result解密结果
     */  
    public static String deCrypt (String content){ 
    	byte[] b1 = parseHexStr2Byte(content);
        KeyGenerator keygen;          
        SecretKey desKey;  
        Cipher c;         
        byte[] cByte = null;
        String result = null;
        
        try {
        	keygen = KeyGenerator.getInstance("AES");  
            keygen.init(128, new SecureRandom(key.getBytes()));  
              
            desKey = keygen.generateKey();  
            c = Cipher.getInstance("AES");  
              
            c.init(Cipher.DECRYPT_MODE, desKey);  
              
              
            cByte = c.doFinal(b1);
            result = new String(cByte,"UTF-8");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
        return   result;
    }  
      
      
    /**2进制转化成16进制 
     * @param buf 
     * @return 
     */  
    private static String parseByte2HexStr(byte buf[]) {  
        StringBuffer sb = new StringBuffer();  
        for (int i = 0; i < buf.length; i++) {  
            String hex = Integer.toHexString(buf[i] & 0xFF);  
            if (hex.length() == 1) {  
                hex = '0' + hex;  
                }  
            sb.append(hex.toUpperCase());  
            }  
        return sb.toString();  
        }  
      
      
    /**将16进制转换为二进制 
     * @param hexStr 
     * @return 
     */       
    private static byte[] parseHexStr2Byte(String hexStr) {   
            if (hexStr.length() < 1)   
                    return null;   
            byte[] result = new byte[hexStr.length()/2];   
            for (int i = 0;i< hexStr.length()/2; i++) {   
                    int high = Integer.parseInt(hexStr.substring(i*2, i*2+1), 16);   
                    int low = Integer.parseInt(hexStr.substring(i*2+1, i*2+2), 16);   
                    result[i] = (byte) (high * 16 + low);   
            }   
            return result;   
    }  
} 